﻿using System;
using System.Linq;
using FluentNHibernate.Conventions;
using FluentNHibernate.Conventions.AcceptanceCriteria;
using FluentNHibernate.Conventions.Inspections;
using FluentNHibernate.Conventions.Instances;
using HashFoo.Core;

namespace HashFoo.Nhib
{
    /// <summary>
    /// Custom class enumeration automapping convention.
    /// </summary>
    /// <remarks>
    /// See: http://wiki.fluentnhibernate.org/Conventions documentation.
    /// </remarks>
    public class EnumerationTypeConvention : IUserTypeConvention
    {
        private static readonly Type OpenType = typeof(EnumerationType<>);

        public void Apply(IPropertyInstance instance)
        {
            instance.CustomType(OpenType.MakeGenericType(instance.Property.PropertyType));
        }

        public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
        {
            // see: http://wiki.fluentnhibernate.org/Acceptance_criteria
            criteria.Expect(x => x.Property.PropertyType.IsClass && x.Property.PropertyType.IsOfGenericBaseType(typeof(Enumeration<>)));
        }
    }
}
